package com.xiyou.luckdraw.api.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xiyou.luckdraw.api.dal.mapper.luckdraw.OrderInfo1Mapper;
import com.xiyou.luckdraw.api.dal.mapper.luckyelite.OrderInfo2Mapper;
import com.xiyou.luckdraw.api.dto.order.OrderInfoDto;
import com.xiyou.luckdraw.api.dto.order.OrderInfoListRequest;
import com.xiyou.luckdraw.api.dto.order.OrderInfoListResponse;
import com.xiyou.luckdraw.api.service.IOrderInfoService;
import com.xiyou.luckdraw.common.constants.ResultCodeConstants;
import com.xiyou.luckdraw.common.exception.ExceptionProcessorUtils;
import com.xiyou.luckdraw.common.utils.DateUtils;
import com.xiyou.luckdraw.common.utils.ReqUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @ClassName: OrderInfoServiceImpl
 * @Description:
 * @author: limingxing
 * @Date: 2019-10-09 21:12
 */
@Slf4j
@Service
public class OrderInfoServiceImpl implements IOrderInfoService {

    @Autowired
    private OrderInfo1Mapper orderInfo1Mapper;
    @Autowired
    private OrderInfo2Mapper orderInfo2Mapper;

    @Override
    public OrderInfoListResponse selectByPrimaryList(OrderInfoListRequest request) {
        log.error("Begin OrderInfoServiceImpl.selectByPrimaryList request : {}", request);
        OrderInfoListResponse response = new OrderInfoListResponse();
        try {
            request.requestCheck();
            PageHelper.startPage(request.getPage(), request.getSize());
            String datasourceType = ReqUtils.getDatasourceType();
            List<OrderInfoDto> orderList = null;
            if (StringUtils.isNotEmpty(request.getStartTime())) {
                request.setStartTime(DateUtils.timeStamp2Date(request.getStartTime(), "YYYY-MM-dd"));
            }
            if (StringUtils.isNotEmpty(request.getEndTime())) {
                request.setEndTime(DateUtils.timeStamp2Date(request.getEndTime(), "YYYY-MM-dd"));
            }
            if(StringUtils.isNotBlank(datasourceType) && "luckdraw".equals(datasourceType)){
                orderList = orderInfo1Mapper.selectByPrimaryList(request);
            }else {
                orderList = orderInfo2Mapper.selectByPrimaryList(request);
            }
            PageInfo pageInfo = new PageInfo(orderList);
            response.setOrderList(orderList);
            response.setTotal(pageInfo.getTotal());
            response.setCode(ResultCodeConstants.SUCCESS.getCode());
            response.setMsg(ResultCodeConstants.SUCCESS.getMessage());
        } catch (Exception e) {
            log.error("OrderInfoServiceImpl.selectByPrimaryList Occur Exception : {}", e);
            ExceptionProcessorUtils.wrapperHandlerException(response, e);
        }
        return response;
    }
}
